package oppsa.view;

import java.awt.Color;
import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;
import java.util.TreeMap;

import oppsa.controller.StatSubSystem;
import oppsa.controller.StatUsers;
import oppsa.controller.Translate;
import oppsa.model.ActionDB;
import oppsa.model.UserDB;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;

public class JChart  {

	private UserDB listeUser;
	private ActionDB listeAction;
	private Translate t = new Translate();
	
	public JChart()	{
		listeUser = new UserDB();
		listeAction = new ActionDB();
	}
	public JFreeChart userAffected() {
		DefaultPieDataset pieDataset = new DefaultPieDataset();
		List<StatUsers> statList= listeUser.getStatUsers();
		
		for (StatUsers su : statList)
		{
			pieDataset.setValue(su.getFirstname()+" "+su.getName(), su.getNb());
		}

		return ChartFactory.createPieChart3D(t.getLocalizedText("lblRespAffected"), pieDataset, false, true, false);
		
	}
	public JFreeChart priority() {
		DefaultPieDataset pieDataset = new DefaultPieDataset();
		TreeMap<Integer, String> mapPriority = listeAction.getStatPriority();
		
		for (Entry<Integer, String> entry : mapPriority.entrySet())
		{
			pieDataset.setValue(entry.getValue(), entry.getKey());
		}

		return ChartFactory.createPieChart3D(t.getLocalizedText("lblActPriority"), pieDataset, false, true, false);
		
	}
	public JFreeChart state() { 
		DefaultPieDataset pieDataset = new DefaultPieDataset();
		TreeMap<Integer, String> mapPriority = listeAction.getStatState();
		
		for (Entry<Integer, String> entry : mapPriority.entrySet())
		{
			pieDataset.setValue(entry.getValue(), entry.getKey());
		}

		return ChartFactory.createPieChart3D(t.getLocalizedText("lblActStatus"), pieDataset, false, true, false);
		
	}

	public JFreeChart SubSys(int nbDays) {
		
	    
		DefaultCategoryDataset barChart = new DefaultCategoryDataset();
		ArrayList<StatSubSystem> listStat = listeAction.getStatSubSys(nbDays);
		
		for (StatSubSystem statSubSystem : listStat)
		{
			barChart.addValue(statSubSystem.getNb() ,statSubSystem.getSubSystemName() ,statSubSystem.getComponentName());
		}

		
		
		JFreeChart chart = ChartFactory.createBarChart3D("Bar Chart Demo", "Category", "Value", barChart, PlotOrientation.VERTICAL, true, true, false);
		CategoryPlot plot = chart.getCategoryPlot();
		plot.setBackgroundPaint(Color.lightGray);
        plot.setDomainGridlinePaint(Color.white);
        plot.setRangeGridlinePaint(Color.white);
        
        
        NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
		
        BarRenderer renderer = (BarRenderer) plot.getRenderer();
        renderer.setDrawBarOutline(false);
        
        CategoryAxis domainAxis = plot.getDomainAxis();
        domainAxis.setCategoryLabelPositions(
            CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 6.0)
        );
        
		return chart;
	}

}
